package com.grandbroad.eim.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.icu.math.BigDecimal;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.entry.PayBean;
import com.grandbroad.eim.entry.PaySuccessBean;
import com.grandbroad.eim.utils.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 *微信支付跳转的页面,通过微信sdk进行跳转，这里只处理回执结果
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String TAG = "WXPayEntryActivity";
    //private PayBean payBean;
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //payBean = (PayBean) getIntent().getExtras().getSerializable("bean");
        api = WXAPIFactory.createWXAPI(this, MyApplication.WX_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX){
            if (resp.errCode == 0){ //支付成功
                //执行支付成功的操作
                ToastUtils.show("成功开通");
                //重新执行,这里访问后台添加一个新订单
               // MyApplication.getMoleclient().getHotVod();
                EventBus.getDefault().post(new PaySuccessBean(false));

            }else if (resp.errCode==0-1){
                Log.e("原因:",resp.transaction);
                Toast.makeText(this,"支付失败，请重试或者联系管理员，错误码:"+resp.errCode,Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new PaySuccessBean(false));
            }else if (resp.errCode==-2)
            {
                ToastUtils.show("支付已取消");
                EventBus.getDefault().post(new PaySuccessBean(false));
            }
            finish();
        }
    }
}



